package model;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;

/**
 * Holds data and and functionality for an Auction
 * 
 * @author Daniel Bartholomew
 */
public class Auction {
	
	/** A date format used to create auction names */
	private static DateFormat my_format = DateFormat.getDateInstance(DateFormat.MEDIUM);
	
	/** The ID number for the next Auction to be created */
	private static int next_id_no = 1;
	
	private final NPO my_npo;
	
	/** A Calendar object representing when the Auction will take place */
	private final Calendar my_date;
	
	private final String my_name;
	
	private final int my_id_no;
	
	private final double my_duration;
	
	private final ArrayList<Item> my_items;
	
	public Auction(final NPO npo, final Calendar date, final double duration) {
		this(npo, date, duration, next_id_no);
	}
	
	protected Auction(final NPO npo, final Calendar date, final double duration, final int id_no) {
		my_npo = npo;
		my_date = date;
		my_duration = duration;
		
		StringBuilder sb = new StringBuilder();
		sb.append(my_npo.getName());
		sb.append(" ");
		sb.append(my_format.format(my_date.getTime()));
		my_name = sb.toString();
		
		my_id_no = id_no;
		if (id_no >= next_id_no) {
			next_id_no = (id_no + 1);
		}
		my_items = new ArrayList<Item>();
	}
	
	public NPO getNPO() {
		return my_npo;
	}
	
	public Calendar getDate() {
		return my_date;
	}
	
	public double getDuration() {
		return my_duration;
	}

	public Collection<Item> getItems() {
		return my_items;
	}
	
	public String getName() {
		return my_name;
	}
	
	public int getID() {
		return my_id_no;
	}
	
	public boolean addItem(Item item) {
		if (!my_items.contains(item)) {
			my_items.add(item);
			return true;
		}
		return false;
	}
	
}
